﻿using DAL;
using System;
using Model;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StudentManagerPro.Student
{
	public partial class EditStudent : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

            if (!IsPostBack)
            {
                //首次加载：初始化班级下拉框
                this.ddlClass.DataSource = new StudentClassService().GetStudentClass();//设置数据源
                this.ddlClass.DataTextField = "ClassName";//设置下拉框显示文本
                this.ddlClass.DataValueField = "ClassId";//设置显示文本对应的值（该值是隐藏的）
                this.ddlClass.DataBind();//调用数据绑定方法


                //根据Id查询并显示
                string studentId = Request.QueryString["StudentId"];
                if (studentId == null)
                {
                    Response.Redirect("~/ErrorPage.html");
                    return;
                }
                else
                {
                    StudentExt student = new StudentService().QueryStudent(studentId);
                    if (student == null)
                    {
                        Response.Redirect("~/ErrorPage.html");
                        return;
                    }
                    else
                    {
                        this.ltaStudentId.Text = studentId;
                        this.txtStuName.Text = student.StudentName;
                        this.txtStuIdNo.Text = student.StudentIdNo;
                        this.txtStuBirthday.Text = student.Birthday.ToString("yyyy-MM-dd");
                        this.txtStuAddress.Text = student.StudentAddress;
                        this.txtPhoneNumber.Text = student.PhoneNumber;
                        this.ddlClass.SelectedValue = student.ClassId.ToString();
                        this.ddlGender.Text = student.Gender;
                    }
                }
            }
            this.ltaMsg.Text = "";
        }

        /// <summary>
        /// 提交修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEditStudent_Click(object sender, EventArgs e)
        {
            //判断身份证号
            StudentService studentService = new StudentService();
            if (studentService.IsExistsStudentIdNo(this.txtStuIdNo.Text.Trim(), this.ltaStudentId.Text.Trim()))
            {
                this.ltaMsg.Text = "<script type='text/javascript'>alert('身份证号：" + this.txtStuIdNo.Text.Trim() + " 以及被其他学员使!')</script>";
                return;
            }



            //3.封装对象
            Students student = new Students
            {
                StudentName = this.txtStuName.Text,
                Gender = this.ddlGender.Text,
                Birthday = Convert.ToDateTime(this.txtStuBirthday.Text),
                ClassId = Convert.ToInt32(this.ddlClass.SelectedValue),
                PhoneNumber = this.txtPhoneNumber.Text,
                StudentAddress = this.txtStuAddress.Text,
                StudentIdNo = this.txtStuIdNo.Text,
                StudentId=Convert.ToInt32(this.ltaStudentId.Text.Trim())
            };
            //4.调用后台
            try
            {
                var result = studentService.UpdateStudentInfoudentId(student);//返回新增的学号

                if (result > 0)
                {
                    Response.Redirect("~/Student/UpLoadImage.aspx?IsModify=1&id=" + student.StudentId, false);//添加成功跳转到上传图片页面
                }
            }
            catch (Exception ex)
            {
                this.ltaMsg.Text = "<script type='text/javascript'>alert('新增学员失败：" + ex.Message + "')</script>";
            }
        }
    }
}